<template>
  <div class="mod-study__notice">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item label="模糊搜索">
        <el-input v-model="state.dataForm.keywords" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('study:notice:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" row-key="id" border style="width: 100%">
      <el-table-column prop="title" label="公告标题" header-align="center"></el-table-column>
      <el-table-column prop="cover" label="公告图片" header-align="center">
        <template v-slot="scope">
          <div style="display: flex;justify-content: center;">
            <el-image style="width: 100px; height: 100px" :src="scope.row.cover" fit="contain" />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="createDate" label="发布时间" header-align="center"></el-table-column>
      <el-table-column prop="author" label="发布人" header-align="center"></el-table-column>
      <el-table-column prop="publishStatusStr" label="公告状态" header-align="center"></el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('study:notice:status') && scope.row.publishStatus === 'PENDING'" type="primary" link @click="updateStatusHandle(scope.row.id)">发布公告</el-button>
          <el-button v-if="state.hasPermission('study:notice:status') && scope.row.publishStatus !== 'PENDING'" type="primary" link @click="updateStatusHandle(scope.row.id)">取消发布</el-button>
          <el-button v-if="state.hasPermission('study:notice:update')" type="primary" link @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="state.hasPermission('study:notice:delete')" type="primary" link @click="state.deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, toRefs } from "vue";
import useView from "@/hooks/useView";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";

const router = useRouter();
const view = reactive({
  getDataListURL: "/study/notice/page",
  getDataListIsPage: true,
  deleteURL: "/study/notice",
  deleteIsBatch: true,
  dataForm: {
    keywords: "",
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const addOrUpdateHandle = (id?: string) => {
  router.push({
    path: '/study/notice/edit',
    query: {
      id
    },
  });
};

const updateStatusHandle = (id: string) => {
  baseService.get('/study/notice/updatePublishStatus', { id }).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          state.getDataList();
        }
      });
    });
}
</script>
